package com.fingard.dsp.bank.directbank.allinpay02;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.allinpay02.util.SybUtil;
import com.fingard.dsp.bank.directbank.bankInterface.IQRPay;
import com.fingard.dsp.bank.reqrespobj.qrpay.ReqGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.qrpay.RetAtsGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;
import com.fingard.text.XmlHelper;

import java.util.TreeMap;

public class AllInPay02QRPay extends AllInPay02Base implements IQRPay {
    public RetAtsGenQRInfo genQRInfo(ReqGenQRInfo p_req) {
        RetAtsGenQRInfo retAts = new RetAtsGenQRInfo();

        retAts.rdSeq = p_req.rdSeq;
        retAts.respCode = "0";
        retAts.respInfo = reqHeader.bankCode + "无此接口";
        retAts.qrInfo = "";

        return retAts;
    }

    public RetAtsUnifiedOrder submitQROrder(ReqUnifiedOrder p_req) {
        RetAtsUnifiedOrder retAts = new RetAtsUnifiedOrder();
        retAts.rdSeq = p_req.rdSeq;

        try {
            ActSetItem actItem = getBankConfig().getActSetItemByAny(p_req.ownAct, p_req.merchantId, p_req.appId, "unifiedorder");
            if (actItem != null) {
                String paytype = "";
                if ("QrWX".equalsIgnoreCase(p_req.reqHeader.commandCode)) {
                    paytype = "W01"; /* 微信扫码支付 */
                } else if ("QrALI".equalsIgnoreCase(p_req.reqHeader.commandCode)) {
                    paytype = "A01"; /* 支付宝扫码支付 */
                } else if ("QrQQ".equalsIgnoreCase(p_req.reqHeader.commandCode)) {
                    paytype = " Q01"; /* 手机QQ扫码支付 */
                }

                String limitPay = "";
                if (StringHelper.hasAnyChar(paytype)) {
                    if ("W01".equals(paytype) || "A01".equals(paytype)) {
                        limitPay = "no_credit";
                    }
                }

                TreeMap<String, String> params = new TreeMap<String, String>();

                params.put("cusid", actItem.merchantId);
                params.put("appid", actItem.appId);
                params.put("version", "11");
                params.put("trxamt", StringHelper.amountMultiply100(p_req.amount));
                params.put("reqsn", p_req.rdSeq);
                params.put("paytype", paytype); /* 交易方式 (W01-微信扫码支付, A01-支付宝扫码支付 , Q01-手机QQ扫码支付) */
                params.put("randomstr", get_nonce_str());
                params.put("body", XmlHelper.encode(p_req.description));
                String attach = new StringBuilder(p_req.attach).substring(0, 50);
                params.put("remark", attach);
                params.put("validtime", p_req.getValidTime());
                params.put("notify_url", actItem.notifyURL);
                params.put("limit_pay", limitPay); /* 支付限制 (暂时只对微信支付和支付宝有效,仅支持no_credit) */
                params.put("sign", SybUtil.sign(params, actItem.saltKey));

                String tmpRetStr = sendUnifiedToBank(params, bankFront.getUrl("unifiedOrderPay"));

                if (StringHelper.hasAnyChar(tmpRetStr)) {
                    if (tmpRetStr.indexOf("{") >= 0) {
                        JSONObject respJson = JSONObject.parseObject(tmpRetStr);

                        String respCode = respJson.getString("retcode");
                        if (respCode.equals("SUCCESS")) {
                            retAts.respCode = "0";
                            retAts.respInfo = respJson.getString("retmsg");
                            retAts.qrInfo = respJson.getString("payinfo");
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = respJson.getString("retmsg");
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpRetStr;
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "通联响应数据为空";
                }
            } else {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息";
                retAts.qrInfo = "";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }

    public RetAtsQueryOrder queryQRPay(ReqQueryOrder p_req) {
        RetAtsQueryOrder retAts = new RetAtsQueryOrder();
        retAts.rdSeq = p_req.rdSeq;

        try {
            ActSetItem actItem = getBankConfig().getActSetItemByAny(p_req.ownAct, p_req.merchantId, p_req.appId, "unifiedorder");
            if (actItem != null) {
                TreeMap<String, String> params = new TreeMap<String, String>();

                params.put("cusid", actItem.merchantId);
                params.put("appid", actItem.appId);
                params.put("version", "11");
                params.put("reqsn", p_req.rdSeq);
                params.put("trxid", "");
                params.put("randomstr", get_nonce_str());
                params.put("sign", SybUtil.sign(params, actItem.saltKey));

                String tmpRetStr = sendUnifiedToBank(params, bankFront.getUrl("unifiedOrderQuery"));

                if (StringHelper.hasAnyChar(tmpRetStr)) {
                    if (tmpRetStr.indexOf("{") >= 0) {
                        JSONObject respJson = JSONObject.parseObject(tmpRetStr);

                        String respCode = respJson.getString("retcode");
                        if ("SUCCESS".equals(respCode)) {
                            retAts.respCode = "0";
                            retAts.outTransID = respJson.getString("trxid");
                            String trxStatus = respJson.getString("trxstatus");
                            retAts.transState = StringHelper.joinNotEmpty("+", respCode, trxStatus);
                            retAts.payInfoCode = trxStatus;
                            if("3889".equals(trxStatus)||"3045".equals(trxStatus)||"3999".equals(trxStatus  )){
                                retAts.payInfoCode = respJson.getString("errmsg");
                            }
                            retAts.respInfo = respJson.getString("retmsg");
                            if(StringHelper.hasAnyChar(respJson.getString("errmsg"))){
                                retAts.respInfo = respJson.getString("errmsg");
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = respJson.getString("retmsg");
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpRetStr;
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "通联联响应数据为空";
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }
}
